package com.epoch.servicecommunication.controller;

import bean.vo.AcceptCom;
import bean.vo.ResultInfo;
import com.epoch.servicecommunication.service.CommunicationService;
import exceptions.ParamsException;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author mi
 */
@RestController
@Slf4j
public class CommunicationController {
    @Resource
    private CommunicationService communicationService;
    /**
     * 添加交流
     * @param communication
     */
    @ApiOperation(value = "发布板块")
    @PostMapping("epoch/stu/saveCommunication")
    public ResultInfo saveCommunication(@RequestBody AcceptCom communication){
        ResultInfo resultinfo=new ResultInfo();
        try{
            communicationService.saveCommunication(communication);
        }catch(ParamsException p){
            p.printStackTrace();
            resultinfo.setCode(p.getCode());
            resultinfo.setMsg(p.getMsg());
        }catch(Exception e){
            e.printStackTrace();
            resultinfo.setCode(500);
            resultinfo.setMsg("添加交流表失败！请稍后重试");
        }
        return resultinfo;
    }

    /**
     * 交流
     * @return Communication
     */
    @ApiOperation(value = "发布板块")
    @GetMapping("epoch/stu/getCommunication/{comId}")
    public ResultInfo getCommunication(@PathVariable("comId") String comId){
        ResultInfo resultinfo=new ResultInfo();
        try{
            resultinfo.setResult(communicationService.getCommunication(comId));
        }catch(ParamsException p){
            p.printStackTrace();
            resultinfo.setCode(p.getCode());
            resultinfo.setMsg(p.getMsg());
        }catch(Exception e){
            e.printStackTrace();
            resultinfo.setCode(500);
            resultinfo.setMsg("返回交流表失败！请稍后重试");
        }
        return resultinfo;
    }
    /**
     * 删除交流
     */
    @ApiOperation(value = "发布板块")
    @DeleteMapping("epoch/stu/deleteCommunication/{id}")
    public ResultInfo deleteCommunication(@PathVariable("id") Integer id){
        ResultInfo resultinfo=new ResultInfo();
        try{
            communicationService.deleteCommunication(id);
        }catch(ParamsException p){
            p.printStackTrace();
            resultinfo.setCode(p.getCode());
            resultinfo.setMsg(p.getMsg());
        }catch(Exception e){
            e.printStackTrace();
            resultinfo.setCode(500);
            resultinfo.setMsg("删除交流表失败！请稍后重试");
        }
        return resultinfo;
    }

    /**
     * 查询交流
     * @param comId
     * @param csId
     * @return Communication
     */
    @ApiOperation(value = "发布板块")
    @GetMapping("epoch/stu/selectCommunication/{comId}/{csId}")
    public ResultInfo getCommunicationById(@PathVariable("comId") String comId,@PathVariable("csId") String csId){
        ResultInfo resultinfo=new ResultInfo();
        try{
            resultinfo.setResult(communicationService.getCommunicationById(comId,csId));
        }catch(ParamsException p){
            p.printStackTrace();
            resultinfo.setCode(p.getCode());
            resultinfo.setMsg(p.getMsg());
        }catch(Exception e){
            e.printStackTrace();
            resultinfo.setCode(500);
            resultinfo.setMsg("查询交流表失败！请稍后重试");
        }
        return resultinfo;
    }
}
